const fs = require('fs');
var request = require('request');

const pageUrl = 'https://test.nbjice.com/b/9010/swagger.json'

// 将远程json下载到本地
// request(pageUrl).pipe(fs.createWriteStream('swagger.json'))

// 写入本地文件
function createCode () {
    fs.readFile('swagger.json', (err, data) => {
        if (err) throw err
        json_obj = JSON.parse(data)
        start_str = `import { getAjax, putAjax, patchAjax, postAjax, deleteAjax } from './myapi' \n` + `const api = { \n`
        fs.writeFile('api/api.js', start_str, (err) => {
            if (err) throw err
        })
        for(var key in json_obj.paths){
            wilte_list = ['/wechatlogin/', '/userinfo/']
            if (key.search("/wechat/.*") !== -1 || wilte_list.indexOf(key) !== -1) {
                if (key.indexOf('{id}') !== -1) {
                    base_path = key.split('{id}')[0]
                } else {
                    base_path = key
                }
                if (json_obj.paths[key].get) {
                    // console.log(key)
                    // console.log(json_obj.paths[key])
                    save_str = `\n`
                    save_str += `\t// ` + json_obj.paths[key].get.description + '\n'
                    fuc_name_list = json_obj.paths[key].get.operationId.split('_')
                    if (fuc_name_list[fuc_name_list.length - 1] == 'list') {
                        save_str +='\t' + json_obj.paths[key].get.operationId + `: (params) => getAjax('${base_path}', params),`
                    } else {
                        save_str +='\t' + json_obj.paths[key].get.operationId + `: (params) => getAjax('${base_path}' + params['id'] + '/', {}),`
                    }
                    fs.writeFile('api/api.js', save_str, { flag:'a' }, (err) => {
                        if (err) throw err
                    })
                }
                if (json_obj.paths[key].post) {
                    save_str = `\n`
                    save_str += `\t// ` + json_obj.paths[key].post.description + '\n'
                    save_str +='\t' + json_obj.paths[key].post.operationId + `: (data) => postAjax('${base_path}', data),`
                    fs.writeFile('api/api.js', save_str, { flag:'a' }, (err) => {
                        if (err) throw err
                    })
                }
                if (json_obj.paths[key].put) {
                    save_str = `\n`
                    save_str += `\t// ` + json_obj.paths[key].put.description + '(整体更新)\n'
                    save_str +='\t' + json_obj.paths[key].put.operationId + `:(data) => putAjax('${base_path}' + data['id'] + '/', data),`
                    fs.writeFile('api/api.js', save_str, { flag:'a' }, (err) => {
                        if (err) throw err
                    })
                }
                if (json_obj.paths[key].patch) {
                    save_str = `\n`
                    save_str += `\t// ` + json_obj.paths[key].patch.description + '(局部更新)\n'
                    save_str +='\t' + json_obj.paths[key].patch.operationId + `:(data) => patchAjax('${base_path}' + data['id'] + '/', data),`
                    fs.writeFile('api/api.js', save_str, { flag:'a' }, (err) => {
                        if (err) throw err
                    })
                }
                if (json_obj.paths[key].delete) {
                    save_str = `\n`
                    save_str += `\t// ` + json_obj.paths[key].delete.description + '\n'
                    save_str +='\t' + json_obj.paths[key].delete.operationId + `: (data) => deleteAjax('${base_path}' + data['id'] + '/', {}),`
                    fs.writeFile('api/api.js', save_str, { flag:'a' }, (err) => {
                        if (err) throw err
                    })
                }
            }
        }
        fs.writeFile('api/api.js', `\n}\n` + `export default api`, { flag:'a' }, (err) => {
            if (err) throw err
        })
    });
}

async function testResult() {
    let result1 = await request(pageUrl).pipe(fs.createWriteStream('swagger.json'))
    console.log(result1)
}

// testResult()
createCode()